<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/alllayui/layui/css/layui.css}" media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<div class="searchTable">
    名称：
    <div class="layui-inline">
        <input class="layui-input" name="userName" id="userName" autocomplete="off">
    </div>
    手机号：
    <div class="layui-inline">
        <input class="layui-input" name="phoneNo" id="phoneNo" autocomplete="off">
    </div>
    <button id="searchBtn" class="layui-btn" data-type="reload">搜索</button>
</div>
<table class="layui-hide" id="User" lay-filter="User"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <shiro:hasPermission name="user/add">
            <button class="layui-btn layui-btn-sm" lay-event="add">新增</button>
        </shiro:hasPermission>
        <shiro:hasPermission name="user/giveRole">
            <button class="layui-btn layui-btn-sm" lay-event="giveRole">赋予角色</button>
        </shiro:hasPermission>
    </div>
</script>

<script type="text/html" id="barDemo">
    <shiro:hasPermission name="user/update">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    </shiro:hasPermission>
    <shiro:hasPermission name="user/delete">
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </shiro:hasPermission>
</script>


<script th:src="@{/alllayui/layui/layui.js}" charset="utf-8"></script>

<script type="text/javascript">
    var btnCls = ['layui-btn-warm', 'layui-btn-danger', 'layui-btn-disabled', 'layui-btn-normal'];

    function dict(rowD, str) {
        var ret = "";
        var btnIndex = 0;
        if (str == "enable") {
            if (rowD.enable == 0) {
                btnIndex = 0;
                ret = "启用";
            }
            if (rowD.enable == 1) {
                btnIndex = 1;
                ret = "禁用";
            }
        }
        if (str == "sex") {
            if (rowD.sex == 0) {
                btnIndex = 1;
                ret = "女";
            }
            if (rowD.sex == 1) {
                btnIndex = 3;
                ret = "男";
            }
        }
        var btn = "<a class=\"layui-btn layui-btn-xs layui-btn-radius " + btnCls[btnIndex] + "\">" + ret + "</a>";
        return btn;
    }
</script>

<script>
    layui.use(['jquery', 'table'], function () {
        var table = layui.table;

        table.render({
            elem: '#User'
            , url: '/user/list'
            , method: 'post'
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                , layEvent: 'LAYTABLE_TIPS'
                , icon: 'layui-icon-tips'
            }]
            , title: 'User'
            , cols: [
                [
                    {type: 'checkbox', fixed: 'left'}
                    , {field: 'id', title: 'ID', align: 'center'}
                    , {field: 'userName', title: '名称', align: 'center'}
                    , {
                    field: 'password', title: '密码', align: 'center', templet: function (d) {
                        return "......"
                    }
                }
                    , {field: 'phoneNo', title: '真实姓名', align: 'center'}
                    , {field: 'mail', title: '年龄', align: 'center'}
                    , {
                    field: 'sex', title: '性别', align: 'center', templet: function (d) {
                        return dict(d, 'sex');
                    }
                }
                    , {
                    field: 'enable', title: '状态', align: 'center', templet: function (d) {
                        return dict(d, 'enable');
                    }
                }
                    , {field: 'addTime', title: '添加时间', align: 'center'}
                    , {field: 'deptId', title: '部门ID', align: 'center'}
                    , {field: 'deptName', title: '部门名称', align: 'center'}
                    , {field: 'info', title: '简介', align: 'center'}
                    , {fixed: 'right', title: '操作', toolbar: '#barDemo'}
                ]
            ]
            , page: true
            , request: {
                pageName: 'currentPageNum' //页码的参数名称，默认：page
                , limitName: 'pageSize' //每页数据量的参数名，默认：limit
            }
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": 0, //解析接口状态
                    "msg": "", //解析提示文本
                    "count": res.totalRecords, //解析数据长度
                    "data": res.recordList //解析数据列表
                };
            }

        });


        var $ = layui.$, active = {
            reload: function () {
                var queryWhere = {};

                var userName$_ = $('#userName');
                if (userName$_.val() != '') {
                    queryWhere['userName'] = userName$_.val();
                }

                var phoneNo$_ = $('#phoneNo');
                if (phoneNo$_.val() != '') {
                    queryWhere['phoneNo'] = phoneNo$_.val();
                }
                //执行重载
                table.reload('User', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: queryWhere
                    , done: function (res, curr, count) {
                        this.where = {};
                    }
                }, 'data');
            }
        };

        $('.searchTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //头工具栏事件
        table.on('toolbar(User)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                //自定义头工具栏右侧图标 - 提示
                case 'add':
                    //iframe层-父子操作
                {
                    var addIndex = layer.open({
                        type: 2,
                        area: ['700px', '450px'],
                        fixed: false, //不固定
                        maxmin: true,
                        content: '/user/add'
                    });
                    layer.full(addIndex);
                }
                    break;

                case 'giveRole': {
                    var ary = checkStatus.data;
                    if (ary.length != 1) break;
                    alert(ary[0].id);
                    var pemissionIndex = layer.open({
                        type: 2,
                        area: ['700px', '450px'],
                        fixed: false, //不固定
                        maxmin: true,
                        content: '/user/giveRole?id=' + ary[0].id
                    });
                    layer.full(pemissionIndex);
                }
                    break;
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            }
            ;
        });

        //监听行工具事件
        table.on('tool(User)', function (obj) {
            var data = obj.data;
            //console.log(obj)
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {

                    $.post("/user/delete", {"id": data.id}, function (data) {
                        obj.del();
                        layer.msg(data.message);
                    }, "json");
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                //iframe层-父子操作
                var updateIndex = layer.open({
                    type: 2,
                    area: ['700px', '450px'],
                    fixed: false, //不固定
                    maxmin: true,
                    content: '/user/update?id=' + data.id
                });
                layer.full(updateIndex);
            }
        });

    });


</script>

</body>
</html>